﻿

#include "snake.h"

//定位控制台位置
void setpos(short x, short y)
{
	COORD pos = { x,y };

	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(handle, pos);
}

//打印欢迎的界面
void welcome()
{
	//第一个界面
	setpos(40, 15);
	printf("欢迎进入贪吃蛇小游戏");
	setpos(0, 28);
	system("pause");
	system("cls");

	//第二个界面
	setpos(28, 16);
	printf("游戏规则：只能使用↑ ↓ ← → 来控制蛇的走向");
	setpos(38, 18);
	printf("所以尽可能获得更多的得分吧");
	setpos(0, 28);
	system("pause");
	system("cls");
}

//打印出游戏地图
void CreatMap()
{
	int i = 0;
	//上
	setpos(0, 0);
	for (i = 0; i < 58; i += 2)
		wprintf(L"%c", WALL);
	//下
	setpos(0, 26);
	for (i = 0; i < 58; i += 2)
		wprintf(L"%lc", WALL);
	//左
    //x是0，y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		setpos(0, i);
		wprintf(L"%c", WALL);
	}
	//x是56，y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		setpos(56, i);
		wprintf(L"%c", WALL);
	}

	  
}

//构建蛇身的结点个数
//void SnakeBody(pSnake ps,int x)
//{
//	pSnakeNode cur = NULL;
//	//初始化蛇身,假如三个节点
//	int i = 0;
//	for (i = 0; i < INITSnakeBody; i++)
//	{
//		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
//		if (cur == NULL)
//		{
//			perror("malloc");
//			return;
//		}
//
//		cur->next = NULL;
//		cur->x = POS_X + i * 2;
//		cur->y = POS_Y;
//
//		//头插法
//		if (ps->_pSnake == NULL)
//		{
//			ps->_pSnake = cur;
//		}
//		else
//		{
//			cur->next = ps->_pSnake;
//			ps->_pSnake = cur;
//		}
//	}
//	
//}

//创建食物
void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;

again:
	//生成随机坐标,第一要在地图里面
	do
	{
		//x的范围在2~54
		x = rand() % 53 + 2;
		//y的范围在1~25
		y = rand() % 25 + 1;

	} while (x % 2 != 0);//并且x坐标必须是为2的倍数

	pSnakeNode cur = ps->_pSnake;
	//第二食物和蛇身不能重复
	while (cur)
	{
		if (cur->x == x && cur->y == y)
			goto again;
		cur = cur->next;
	}

	//就可以创建食物了
	pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (food == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	else
	{
		food->x = x;
		food->y = y;
		setpos(food->x, food->y);
		wprintf(L"%lc", FOOD);
		ps->_pFood = food;
	}
	
}

//初始化蛇的所有信息
void INITSNAKE(pSnake  ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	//初始化蛇身的结点
	for (i = 0; i < INITSnakeBody; i++)
	{
		//创建蛇⾝的节点
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		//设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		//头插法
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}

	//打印出蛇身
	cur = ps->_pSnake;

	while (cur)
	{
		setpos(cur->x, cur->y);
		wprintf(L"%lc",BODY);
		cur = cur->next;
	}

	//初始化蛇的一些数据
	ps->_Dir = RIGHT;
	ps->_foodWeight = 10;
	ps->_SleepTime = 200;
	ps->_Socre = 0;
	ps->_Status = OK;

	
}

//设计游戏开始界面
void GameStar(pSnake ps)
{
	//设置控制台窗⼝的⻓宽：设置控制台窗⼝的⼤⼩，30⾏，100列
	system("mode con cols=120 lines=30");
	system("title 贪吃蛇");

	//消除光标
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//影藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态

	//打印界面欢迎信息
	welcome();

	//打印出游戏地图
	CreatMap();

	//初始化蛇身
	INITSNAKE(ps);

	//创建食物
	CreatFood(ps);
}

//打印出自身的信息
void SnakeFrint()
{
	setpos(60, 16);
	printf("游戏规则：只能使用↑ ↓ ← → 来控制蛇的走向");
	setpos(70, 17);
	printf("按F3加速，食物得分将增大");
	setpos(70, 18);
	printf("按F4减速，食物得分将减小");
}


void pause()
{
	while (1)
	{
		
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;

		}
	}
}

//判断下一个节点是否为食物
int NextIsFood(pSnakeNode pNextNode, pSnake ps)
{
	return (pNextNode->x == ps->_pFood->x) && (pNextNode->y == ps->_pFood->y);
}

//吃掉食物
void EatFood(pSnakeNode pNextNode, pSnake ps)
{
	//头插法
	pNextNode->next = ps->_pSnake;
	ps->_pSnake = pNextNode;

	//打印蛇
	pSnakeNode cur = ps->_pSnake;

	while (cur)
	{
		setpos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//吃完加分
	ps->_Socre += ps->_foodWeight;
	free(ps->_pFood);

	//重新创造一个食物
	CreatFood(ps);
}

void NoFood(pSnakeNode pNextNode, pSnake ps)
{
	//头插法
	pNextNode->next = ps->_pSnake;
	ps->_pSnake = pNextNode;

	//删除最后一个节点，并且打印出空格
	pSnakeNode cur = ps->_pSnake;

	while (cur->next->next)
	{
		setpos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//打印最后一个蛇身的结点
	setpos(cur->x, cur->y);
	wprintf(L"%lc", BODY);
	//最后⼀个位置打印空格，然后释放节点
	setpos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;

}

int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}

int KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;

	while (cur)
	{
		if ((cur->x == ps->_pSnake->x) && (cur->y == ps->_pSnake->y))
		{
			ps->_Status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}


void SnakeMove(pSnake ps)
{
	//创建下⼀个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}

	//确定下⼀个节点的坐标，下⼀个节点的坐标根据，蛇头的坐标和⽅向确定
	
	switch (ps->_Dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}

	//如果下⼀个位置就是⻝物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else//如果没有⻝物
	{
		NoFood(pNextNode, ps);
	}

	KillByWall(ps);
	KillBySelf(ps);
}

//设计游戏运行界面
void GameRun(pSnake ps)
{
	//打印出自身的信息
	SnakeFrint();
	do
	{
		setpos(60, 10);
		printf("得分：%d\t此时每个食物价值：%d", ps->_Socre, ps->_foodWeight);

		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();//意思就是休眠久一点
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_SleepTime >= 50)
			{
				ps->_SleepTime -= 30;
				ps->_foodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_SleepTime < 350)
			{
				ps->_SleepTime += 30;
				ps->_foodWeight -= 2;
				if (ps->_SleepTime == 350)
				{
					ps->_foodWeight = 1;
				}
			}
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//当按ESC为正常退出
			ps->_Status = END_ESC;
			break;
		}

		//蛇每次⼀定之间要休眠的时间，时间短，蛇移动速度就快
		Sleep(ps->_SleepTime);

		SnakeMove(ps);

	} while (ps->_Status==OK);
}

//设计游戏结束界面
void GameEed(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;

	//判断游戏结束的原因
	setpos(24, 12);
	switch (ps->_Status)
	{
	case KILL_BY_WALL:
		printf("很遗憾，你撞到墙了");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，你咬到自己了");
		break;
	case END_ESC:
		printf("以主动退出");
		break;
	}

	//将蛇身节点释放
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}

	setpos(0, 27);
}
